Imports Q7P1TTS.Lib

Public Class LoginForm
    Private Sub LoginForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        cboModeSelector.SelectedIndex = 0
    End Sub

    Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click
        'Me.Parent.Focus()
        If validateData() Then
            If txtAccount.Text.Trim() = My.Settings("SYSTEM_ADMIN").ToString() And txtPassword.Text.Trim() = My.Settings("SYSTEM_PASSWORD").ToString() Then
                SessionHelper.Sessions("IS_ADMIN") = True
                Dim mainForm As MainForm = Me.Owner
                mainForm.ReloadMenu()
                Me.Close()
            Else
                Dim acc As New Object()
                If cboModeSelector.SelectedItem.ToString() = "Customer" Then
                    acc = CustomerController.Login(txtAccount.Text.Trim(), txtPassword.Text.Trim())
                ElseIf cboModeSelector.SelectedItem.ToString() = "Employee" Then
                    acc = EmployeeController.Login(txtAccount.Text.Trim(), txtPassword.Text.Trim())
                End If
                If acc Is Nothing Then
                    MessageBox.Show("Account or Password is invalid. Please try again", "Login failed", MessageBoxButtons.OK, MessageBoxIcon.Error)
                Else
                    SessionHelper.Sessions("IS_USER") = True
                    SessionHelper.Sessions("USER_INFO") = acc
                    Dim mainForm As MainForm = Me.Owner
                    mainForm.ReloadMenu()
                    Me.Close()
                End If
            End If
            
        End If
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        'Me.Parent.Focus()
        Me.Close()
    End Sub
    Private Function validateData() As Boolean
        Dim res As Boolean = True
        If String.IsNullOrEmpty(txtAccount.Text.Trim()) Then
            errorProvider.SetError(txtAccount, "Account can not be left blank!")
            res = res And False
        End If
        If String.IsNullOrEmpty(txtPassword.Text.Trim()) Then
            errorProvider.SetError(txtPassword, "Password can not be left blank!")
            res = res And False
        End If
        Return res
    End Function

    Private Sub btnRegister_LinkClicked(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRegister.LinkClicked
        Try
            Dim mainForm As MainForm = Me.Owner
            mainForm.GoToForm(New CustomerForm(), "Registration")
            Me.Close()
        Catch ex As Exception

        End Try
    End Sub
End Class
